using TMPro;
using UnityEngine;
using UnityEngine.EventSystems;
using UnityEngine.UIElements;
using Image = UnityEngine.UI.Image;

public class AStarNodeMono : MonoBehaviour,IPointerEnterHandler
{
    public AStarNode node;
    public Color orignColor;

    public GameObject root;
    public Image img;
    public TextMeshProUGUI G;
    public TextMeshProUGUI H;
    public TextMeshProUGUI F;
    public TextMeshProUGUI Arrow;

    // Start is called before the first frame update
    void Start()
    {
        root.SetActive(false);
    }

    // Update is called once per frame
    void Update()
    {
    }
    
    private void OnMouseOver()
    {
       
    }
    private void UpdateColor()
    {
        if (node.isBlock)
        {
            SetColor(Color.cyan);
        }
        else
        {
            SetColor(orignColor);
        }
    }

    public void SetColor(Color color)
    {
        //GetComponent<MeshRenderer>().material.color = color;
        img.color = color;
    }

    public void ResetColor()
    {
        //GetComponent<MeshRenderer>().material.color = orignColor;
        img.color = orignColor;
        root.SetActive(false);
    }

    /// <summary>
    /// 设置ghf值
    /// </summary>
    /// <param name="g"></param>
    /// <param name="h"></param>
    /// <param name="f"></param>
    /// <param name="dir"></param>
    /// <param name="isChange"></param>
    public void SetGHF(int g, int h, int f, Vector2Int dir,bool isChange=false)
    {
        root.SetActive(true);
        G.text = $"G:{g}";
        H.text = $"H:{h}";
        F.text = $"F:{f}";

        float rotate = 0;
        
        if (dir.x == 0)
        {
            if (dir.y > 0)
            {
                rotate = 90;
            }
            else
            {
                rotate = -90;
            }
        }
        
        if (dir.y == 0)
        {
            if (dir.x > 0)
            {
                rotate = 0;
            }
            else
            {
                rotate = -180;
            }
        }

        if (dir.x > 0 && dir.y > 0)
        {
            rotate = 45;
        }
        if (dir.x < 0 && dir.y > 0)
        {
            rotate = 135;
        }
        
        if (dir.x < 0 && dir.y < 0)
        {
            rotate = -135;
        }
        
        if (dir.x > 0 && dir.y < 0)
        {
            rotate = -45;
        }

        Arrow.transform.localEulerAngles = new Vector3(0, 0, rotate);
        if (isChange)
        {
            Arrow.color = Color.green;
        }
    }
    
    public void UpdateBlock()
    {
        if (node == null)
        {
            return;
        }
        if (Input.GetKey(KeyCode.Q))
        {
            if (!node.isBlock)
            {
                node.isBlock = true;
                UpdateColor();
            }
        }
        if (Input.GetKey(KeyCode.W))
        {
            if (node.isBlock)
            {
                node.isBlock = false;
                UpdateColor();
            }
        }
    }

    public void OnPointerEnter(PointerEventData eventData)
    {
        UpdateBlock();
    }
}